<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
String path=request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>后台管理</title>
	<jsp:include page="/static/commonjsp/head-js.jsp" flush="true"/>
</head>
<body>
	<div class="x-body">
        <form action="" method="post" class="layui-form layui-form-pane">
                <div class="layui-form-item">
                    <label for="name" class="layui-form-label">
                        <span class="x-red">*</span>角色名
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="name" name="roleName" required="" lay-verify="required"
                        autocomplete="off" class="layui-input" value="">
                    </div>
                </div>
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">
                       	 拥有权限
                    </label>
                    <table  class="layui-table layui-input-block">
                        <tbody>
                        	<c:forEach items="${urls}" var="item">
                        		<tr>
	                                <td style="width:120px;">
	                                    <input type="checkbox" name="urlid[]" lay-skin="primary" 
	                                    title="${item.title}" 	value="${item.id}">
	                                </td>
	                                <td>
	                                    <div class="layui-input-block">
	                                    	<c:forEach items="${item.childMenu}" var="childItem">
		                                        <input name="urlid[]" lay-skin="primary" type="checkbox" title="${childItem.title}" 
		                                        		value="${childItem.id}" > 
	                                    	</c:forEach>
	                                    </div>
	                                </td>
                           		</tr>
                        	</c:forEach>
                        </tbody>
                    </table>
                </div>
                <div class="layui-form-item layui-form-text">
                    <label for="desc" class="layui-form-label">
                      	  描述
                    </label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入内容" id="desc" name="desc" class="layui-textarea"></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                <button class="layui-btn" lay-submit="" lay-filter="add">确定</button>
              </div>
            </form>
    </div>
    <script>
        layui.use(['form','layer'], function(){
            $ = layui.jquery;
          var form = layui.form
          ,layer = layui.layer;
        
          //自定义验证规则
          form.verify({
            nikename: function(value){
              if(value.length < 5){
                return '昵称至少得5个字符啊';
              }
            }
            ,pass: [/(.+){6,12}$/, '密码必须6到12位']
            ,repass: function(value){
                if($('#L_pass').val()!=$('#L_repass').val()){
                    return '两次密码不一致';
                }
            }
          });

          //监听提交
          form.on('submit(add)', function(data){
            var field = data.field;
            //正则匹配出urlid
            var reg = new RegExp(/urlid/);
            var urlid = "";
            var content = new Array();
            
            for(var key in field){
            	if(key.match(reg)){
            		urlid += field[key] +",";
            	}else{
            		content[key] = field[key];
            	}
            }
            content["urlid"] = urlid;
            
            //发起异步
            $.ajax({
            	url:"${pageContext.request.contextPath}/admin/roleadd",
				type:"POST",
				data:{'id':content['id'],'roleName':content['roleName'],'urlid':content["urlid"],'roleInformation':content['desc']},
				dataType:'json',
				success:function(result){
					if(result.success){
					 	layer.alert(result.msg, {icon: 6},function () {
			                // 获得frame索引
			                var index = parent.layer.getFrameIndex(window.name);
			                //关闭当前frame
			                parent.layer.close(index);
			            });
					}else{
						layer.alert(result.msg, {icon: 5},function () {
			                // 获得frame索引
			                var index = parent.layer.getFrameIndex(window.name);
			                //关闭当前frame
			                parent.layer.close(index);
			            });
					}
				},
            	error:function(){
            		layer.alert("请求服务器失败！", {icon: 5},function () {
		                // 获得frame索引
		                var index = parent.layer.getFrameIndex(window.name);
		                //关闭当前frame
		                parent.layer.close(index);
		            });
            	}
            })
            return false;
          });
          
          
        });
    </script>
  </body>
</html>

